<?php
require_once 'application/models/Coloc.php';

class ColocController extends Zend_Controller_Action 
{
    //TODO: interdit si non loggué, sinon liste des coloc du gugus
	function indexAction()
    {
		HttpUtil::do405();
    }
    
    
    function getAction()
    {
		//TODO: check rights pour chaque fonction (Zend_Acl ?)
		
		
		// get params
		$id = $this->getRequest()->getParam('id');
		
		// get Coloc
	    $coloc = new Coloc();
		$where = $coloc->getAdapter()->quoteInto('id = ?', $id);
	    $colocRows = $coloc->fetchAll($where);
	    
	    //check asnwer : 404 if not found
	    if (isset($colocRows[0])) {
	        $this->_helper->json($coloc->getArrayFromRow($colocRows[0]));
	    }else{
			HttpUtil::do404();
	    }
    }
    
    
    
    function postAction()
    {
		//TODO: check rights pour chaque fonction
		

		
		// get params
		$name = $this->getRequest()->getParam('name');
		$passwordDigest = $this->getRequest()->getParam('passwordDigest');
		$alias = $this->getRequest()->getParam('alias');
		
		$table = new Coloc();
		
		// check params type
		//TODO: check presence des autres elements
		$isValid = TypeChecker::validate(array(
			array('data' => $name, 'type' => TypeChecker::$_ALNUM_TYPE, 'name' => 'name')
		));
		if ($isValid !== true) {
			HttpUtil::do400('validation', $isValid);
		}
		
		// check that the name doesn't exist
		$where = $table->getAdapter()->quoteInto('name = ?', $name);
	    $colocRows = $table->fetchAll($where);
	    if (isset($colocRows[0])) {
			HttpUtil::do400('name_exist');
	    }
		
		
		//TODO: check injection SQL (ca a l'air good)
		
	    // insert the new coloc
		try {
			$data = array(
			    'name' => $name,
			    'password_digest' => $passwordDigest,
			    'alias'      => $alias
			);
			 
			$lastInsertId = $table->insert($data);	    
		}catch(Exception $exception) {
			HttpUtil::do400('creation_failed');
		}
	    
		//TODO: prendre en compte 201 (l'helper json annule cet appel)
		HttpUtil::set201();

		// get created Coloc
		$table = new Coloc();
		$where = $table->getAdapter()->quoteInto('id = ?', $lastInsertId);
	    $colocRows = $table->fetchAll($where);
	    
	    // return the created coloc
	    if (isset($colocRows[0])) {
	    	$this->_helper->json($table->getArrayFromRow($colocRows[0]));
	    }else{
			HttpUtil::do400('creation_failed');
	    }
    }

    
    
    
    function putAction()
    {
		//TODO: check rights pour chaque fonction
		

		
		// get params
		$id = $this->getRequest()->getParam('id');
   		$alias = $this->getRequest()->getParam('alias');
		
		// check params type
		$isValid = TypeChecker::validate(array(
		));
		if ($isValid !== true) {
			HttpUtil::do400('validation', $isValid);
		}
   				
		// update table
		try {
		
			$table = new Coloc();
			 
			$data = array(
			    'alias'      => $alias
			);
			 
			$where = $table->getAdapter()->quoteInto('id = ?', $id);

			$numberOfRows = $table->update($data, $where);

		}catch(Exception $exception) {
			HttpUtil::do400('update_failed');
		}
		// we do not check the number of rows, 'cause in the case of putting the same
		// data that the existing one : then, numberofrows = 0 (although it's a success)
	    

		// get updated Coloc
		$table = new Coloc();
		$where = $table->getAdapter()->quoteInto('id = ?', $id);
	    $colocRows = $table->fetchAll($where);
		
	    // return the updated coloc
	    if (isset($colocRows[0])) {
	    	$this->_helper->json($table->getArrayFromRow($colocRows[0]));
	    }else{
			HttpUtil::do400('update_failed');
	    }
    }
    
    
    
    function deleteAction()
    {
		//TODO: check rights pour chaque fonction
		

		
		// get params
		$id = $this->getRequest()->getParam('id');
		
		try {
		
			$table = new Coloc();
			 
			$where = $table->getAdapter()->quoteInto('id = ?', $id);

			$numberOfRows = $table->delete($where);
			
		}catch(Exception $exception) {
			HttpUtil::do400('delete_failed');
		}
	    
	    // check the number of rows deleted
	    if ($numberOfRows < 1) {
			HttpUtil::do404();
	    }
    }
}
